package com.istudy.service.imp;

import com.istudy.dao.BaseDao;
import com.istudy.dao.interfaces.VideoDao;
import com.istudy.dao.impl.VideoDaoImpl;
import com.istudy.dao.xyjdao.impl.VideoImplDao;
import com.istudy.entity.User;
import com.istudy.entity.Video;
import com.istudy.service.interfaces.VideoService;
import com.istudy.utils.JDBCUtils;
import com.istudy.utils.Page;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 视频业务逻辑层
 *
 * @author xdd
 * @create 2020-08-24 16:59
 */
public class VideoServiceImpl implements VideoService {
    VideoImplDao videoImplDao=new VideoImplDao(JDBCUtils.getConnection());//获取视频持久层对象

    /**
     * 根据视频类型id获取播放列表
     *
     * @return 视频播放列表
     */
    @Override
    public List<Video> selectVideoAll(int type){
        List<Video> videos=null;
        try {
            videos= videoImplDao.selectVideoAll(type);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return videos;
    }
    /**
     * 获取播放列表
     *
     * @return 视频播放列表
     */
    @Override
    public List<Video> getVideoList(HttpServletRequest request, int num,int type) {
        Connection connection = null;
        Page page=new Page();
        List<Video> list = new ArrayList<>();
        User user=(User)request.getSession().getAttribute("user");
        try {
            connection = BaseDao.getConnection();
            VideoDao videoDao = new VideoDaoImpl();
            page.setPageSize(8);
            page.setCount(videoDao.getCount(type));
            if (num!=0){
                page.setPageNo(num);
            }
            list= videoDao.getshowlist(page,type);
            request.setAttribute("page",page);
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据视频类型id获取视频
     *
     * @param id
     * @return video
     */
    @Override
    public Video selectVideoById(int id) {
        Connection connection = null;
        Video video = new Video();
        try {
            connection = BaseDao.getConnection();
            VideoDao videoDao = new VideoDaoImpl();
           video= videoDao.querybyId(id);
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return video;
    }

    /**
     * 根据视频名称获取视频列表
     *
     * @param request
     * @param name
     * @return video
     */
    @Override
    public List<Video> getVideoListByName(HttpServletRequest request,int num, String name) {
        Connection connection = null;
        Page page=new Page();
        List<Video> list = new ArrayList<>();
        try {
            connection = BaseDao.getConnection();
            VideoDao videoDao = new VideoDaoImpl();
            page.setPageSize(8);
            page.setCount(videoDao.getCount(name));
            if (num!=0){
                page.setPageNo(num);
            }
            list= videoDao.getVideoListByName(name,page);
            request.setAttribute("page",page);
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 新增视频
     *
     * @param video
     * @return
     */
    @Override
    public boolean insertVideo(Video video) {

            Connection connection = null;
            int count = 0;
            boolean flag = false;
            try {
                connection = BaseDao.getConnection();
              VideoDao videoDao=new VideoDaoImpl();
                count = videoDao.insertVideo(video);
                if(count > 0){
                    flag = true;
                }
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return flag;

    }
}
